<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>

<!-- 购物车弹出菜单 -->
<div class="shopping-popup-menu">
    <div class="drug-data">
        <p class="drug-name-spec"></p>
        <p class="drug-price">￥<em class="_realprice"></em></p>
        <a class="popup-close" href="javascript:;" onclick="menu_close()"></a>
    </div>
    <ul>
        <li>库存：<em class="_inventory"></em></li>
        <li>
            <span class="buy-num">购买数量</span>
            <div class="count">
                <div class="count-box">
                    <i class="sy_minus">-</i>
                    <input class="num focus-auto-select" name="amount" onchange="inputAmount(this)" type="number"
                           value="1" maxnum="1">
                    <i class="sy_plus">+</i>
                </div>
            </div>
        </li>
        <li>单品合计：<em>￥<em class="_totalPrice">0</em></em></li>
    </ul>
    <a href="javascript:;" onclick="addToCart()">确定</a>
</div>

<!-- 遮罩层 -->
<div class="mask"></div>

<script>
    var priceid;
    //弹出菜单
    function stopBubble(event, id) {
        priceid = id;

        $w.httpRequest({
            url: "${ctx}/we/order/cart/" + id,
            method: "get",
            ok: function (ret) {

                $(".mask").show(); //显示遮罩层

                var product = ret.Data;
                $(".drug-name-spec").text(product.name + " " + product.b_guige);
                var price = product.price;
                if (${islogin}) {
                    if (${isuserauth}) {
                        $("._realprice").text(price);
                    } else {
                        $(".drug-price").text("审核通过可见");
                    }
                } else {
                    $(".drug-price").text("登录可见");
                }

                var amount = product.amount;
                if (amount <= 0) {
                    $("._inventory").text("无");
                    $(".num").attr("maxnum", 0);
                    $(".num").val(0);
                } else if (amount <= 100) {
                    $("._inventory").text(amount).css("color","#FF6767");
                    $(".num").val(1);
                    $(".num").attr("maxnum", amount);
                } else {
                    $("._inventory").text(">100").css("color","#999");
                    $(".num").val(1);
                    $(".num").attr("maxnum", amount);
                }

                var num = $(".num").val();
                $("._totalPrice").text(price * num);
            }
        });
        var toast = new auiToast({});
        toast.loading({
            title: "加载中",
            duration: 500
        }, function (ret) {
            setTimeout(function () {
                $(".shopping-popup-menu").show();
                toast.hide();
            }, 500)
        });
        event.stopPropagation();//阻止冒泡
    }

    //关闭菜单
    function menu_close() {
        $(".mask").hide();
        $(".shopping-popup-menu").hide();
    }
    //点击遮罩层，关闭菜单
    $(".mask").click(function () {
        $(".mask").hide();
        $(".shopping-popup-menu").hide();
    })

    //减少商品数量
    $(".sy_minus").click(function () {
        var txt = $(this).next();
        var n = parseInt(txt.val());
        n--;
        txt.val(n);
        inputAmount(txt);
    });

    //输入数量
    $('.num').change(function () {
        inputAmount(this);
    });

    //数量焦点
    $('.num').focus(function () {
        $(this).select();
    });

    //增加商品数量
    $(".sy_plus").click(function () {
        var txt = $(this).prev();
        var n = parseInt(txt.val());
        n++;
        txt.val(n);
        inputAmount(txt);
    });

    function inputAmount(obj) {
        $(obj).val($(obj).val().replace(/[^0-9]/g, ''));
        if ($(obj).val() == '0' || $(obj).val() == '') $(obj).val('1');
        if ($(obj).attr("maxnum") && $(obj).val()) {
            var max = parseInt($(obj).attr("maxnum"));
            if (parseInt($(obj).val()) > max) $(obj).val(max);
        }
        var num = parseInt($(obj).val());
        var price = parseInt($("._realprice").text()*100);
        $("._totalPrice").text(parseFloat(num * price/100));
    }

    var cartSubmit = false;
    function addToCart() {
        if (cartSubmit) {
            return false;
        }
        cartSubmit = true;
        var product = priceid + "_" + $(".focus-auto-select").val();
        var productAttr = [];
        productAttr.push(product);
        $w.httpRequest({
            url: "${ctx}/we/home/addToCart",
            data: {"idsAmounts": productAttr},
            ok: function () {
                $w.httpRequest({
                    url:"${ctx}/order/getCartNum",
                    method:"get",
                    ok:function (ret) {
                        if(ret.Data) {
                            $("._bottomCartNum").text(ret.Data);
                        }else{
                            $("._bottomCartNum").removeClass("num");
                        }
                    }
                })
                menu_close();
                new auiToast().success({
                    title: "加入购物车成功",
                    duration: 800
                })
            },
            fail: function (ret) {
                menu_close();
                new auiToast().fail({
                    title: "加入购物车失败",
                    duration: 800
                });
            },
            all: function () {
                cartSubmit = false;
            }
        })
    }
</script>